package com.nbk.composition

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.toolkit.support.SFunction
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.nbk.composition.dao.bean.*
import com.nbk.composition.dao.repository.*
import com.nbk.composition.manager.TeacherAllot
import com.nbk.composition.utils.getCurrentTime
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.stereotype.Component

/**
 * Author:  LuoHaoLun
 * Email :  506503279@qq.com
 * Date  :  2021/5/24
 */
@Component
class InitCache : CommandLineRunner {

    @Autowired
    private lateinit var teacherMapper: TeacherMapper

    @Autowired
    private lateinit var compositionMapper: CompositionMapper


    override fun run(vararg args: String?) {
        val queryTeacher = KtQueryWrapper(Teacher::class.java).apply { select(Teacher::id) }
        val teacherIds: List<Int> = teacherMapper.selectObjs(queryTeacher) as List<Int>
        val teacherMap = teacherIds.map { id ->
            val queryComposition = KtQueryWrapper(Composition::class.java).apply {
                eq(Composition::teacherId, id)
                eq(Composition::state, "待批改")
            }
            id to compositionMapper.selectCount(queryComposition)
        }.toMap()
        TeacherAllot.initTeacherMap(teacherMap)
    }
}